<template>
	<comRoot>
		<template #page>
			
			<image v-if="commonConfigs && commonConfigs.domainFrontFileLink" :src="`${commonConfigs.domainFrontFileLink}/files/images/page-bg.png`" class="bg" />
			
			<comDetailNavigateBack :title="detailTitle" />
			
			<comScrollTab v-model="tabIndex" :list="tabAreaList">
				<scroll-view scroll-y :style="{ height: 'calc(100vh - ' + commonNavHeight + ' - 120rpx)' }" @scrolltolower="scrolltolower" v-if="resLists && resLists.length">
					<view class="relative" v-for="(item, index) in resLists" :key="index">
						<comListInfo :fromPage="type"  :title="item.title" :value="item.value" :date="item.date" v-if="resLists.length" :type="item.type"></comListInfo>
					</view>
					<comLoadMore :loading="loading" :showLoadendTxt="showLoadendTxt"></comLoadMore>
				</scroll-view>
				<comEmptyData v-else-if="resLists && !resLists.length && !loading"></comEmptyData>
				<comLoading style="width: 100%;height: 100%;" v-if="loading"></comLoading>
			</comScrollTab>
		</template>
	</comRoot>
</template>

<script>
	import store from '@/store';
	import paging from '@/inmixed/paging.mixin.js';
	import { getUserCurrencyListApi, getUserPointsListApi, getUserBillApi } from '@/setupproject/INTERFACE';
	export default {
		name: 'userBillListPage',
		mixins: [paging],
		data() {
			return {
				loading: true,
				tabIndex: 0,
				type: '',
				detailTitle: '',
				tabAreaList: [
					{ text: '全部', type: 0 },
					{ text: '支出', type: 1 },
					{ text: '收入', type: 2 }
				],
				
			};
		},
		onLoad({ type }) {
			this.type = type;
			if(type === 'bill') this.detailTitle = '我的账单';
			if(type === 'balance') this.detailTitle = '机能记录';
			if(type === 'currency') this.detailTitle = '飞机币记录';
			if(type === 'integral') this.detailTitle = '积分记录';
		},
		watch: {
			tabIndex() {
				this.resLists = [];
				this.pagingInitFun();
				this.resListsGetFun();
			}
		},
		computed: {
			commonNavHeight() {
				return store.getters.commonNavHeight + 'px';
			},
			commonConfigs() {
				return store.getters.commonConfigs || {};
			},
			showLoadendTxt() {
				return this.isShowPageLoadedTxt && !this.loading && this.resLists.length ? true : false;
			}
		},
		methods: {
			async listsGetFun() {
				this.loading = true;
				let params = { ...this.pageInfoObj };
				this.tabIndex && (params.type = this.tabIndex);
				
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = routes[routes.length - 1].route; //获取当前页面路由
				let curParam = routes[routes.length - 1].options; // 获取参数
				
				let res = {};
				if(curParam.type === 'bill' || curParam.type === 'balance') {
					res = await getUserBillApi(params);
				}
				
				if(curParam.type === 'currency') {
					res = await getUserCurrencyListApi(params);
				}
				
				if(curParam.type === 'integral') {
					res = await getUserPointsListApi(params);
				}
				
				setTimeout(()=>{ this.loading = false },0)
				if (res.code !== 200) {
					return uni.showToast({ title: res.msg, icon: 'none' });
				}
				
				if(curParam.type === 'bill' || curParam.type === 'balance') {
					res.data.resLists = res.data.bill.map((item) => {
						return {
							title: item.scene_desc,
							date: item.scene_time,
							value: item.money,
							type: item.type
						};
					});
				}
				
				if(curParam.type === 'currency') {
					res.data.resLists = res.data.currency_list.map((item) => {
						return {
							value: item.value,
							type: item.type,
							title: item.scene_desc,
							date: item.scene_time,
						};
					});
				}
				
				if(curParam.type === 'integral') {
					res.data.resLists = res.data.points_list.map((item) => {
						return {
							title: item.scene_desc,
							date: item.scene_time,
							value: item.value,
							type: item.type
						};
					});
				}

				return res.data;
			}
		}
	};
</script>
